package com.tanhua.dubbo.api.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.dubbo.api.UserInfoApi;
import com.tanhua.dubbo.mapper.UserInfoMapper;
import com.tanhua.model.domain.UserInfo;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Map;

@DubboService
public class UserInfoApiImpl implements UserInfoApi {

    @Autowired
    private UserInfoMapper userInfoMapper;

    @Override
    public void save(UserInfo userInfo) {
        userInfoMapper.insert(userInfo);
    }

    @Override
    public void update(UserInfo userInfo) {
        userInfoMapper.updateById(userInfo);
    }

    @Override
    public UserInfo findById(Long userID) {
        UserInfo userInfo = userInfoMapper.selectById(userID);
        return userInfo;
    }

    @Override
    public Map<Long, UserInfo> findByIds(List<Long> userIds) {
        QueryWrapper<UserInfo> userInfoQueryWrapper = new QueryWrapper<>();
        userInfoQueryWrapper.in("id",userIds);
        List<UserInfo> userInfoList = userInfoMapper.selectList(userInfoQueryWrapper);
        Map<Long, UserInfo> mapResult = CollUtil.fieldValueMap(userInfoList, "id");
        return mapResult;
    }

    @Override
    public Page<UserInfo> findFriendByPage(Page<UserInfo> page, List<Long> friendIds, String keyword) {
        QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("id",friendIds);
        if(!StringUtils.isEmpty(keyword)) {
            queryWrapper.like("nickname", keyword);
        }
        userInfoMapper.selectPage(page,queryWrapper);
        return page;
    }

    @Override
    public Page<UserInfo> findByPage(Integer page, Integer pagesize) {
        Page<UserInfo> pageInfo = new Page<>(page,pagesize);
        userInfoMapper.selectPage(pageInfo,null);
        return pageInfo;
    }
}
